package com.example.orderapplication.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.example.orderapplication.R
import com.example.orderapplication.bean.DataX
import java.util.regex.Pattern


/**
 * @author: Xu QiHao
 * @date: 2021/10/2116:59
 * @Description：
 */
class HomeItemAdapter(context: Context, foodDatas: List<DataX>?) :
    BaseAdapter() {
    private val context: Context = context
    private val foodDatas: List<DataX>? = foodDatas


    override fun getCount(): Int {
        return foodDatas?.size ?: 10
    }

    override fun getItem(position: Int): Any {
        return foodDatas!!.size
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    @SuppressLint("ViewHolder")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var viewHold: ViewHold? = null
        var v: View? = null
        val subcategory: DataX = foodDatas!![position]
        if (convertView == null) {
            viewHold = ViewHold()
            v = View.inflate(context, R.layout.item_home_category, null)
            viewHold.tvName = v.findViewById(R.id.item_home_name)
            viewHold.ivIcon = v.findViewById(R.id.item_album)
            viewHold.ivJia = v.findViewById(R.id.iv_jia)
            viewHold.ivJian = v.findViewById(R.id.iv_jian)
            viewHold.tvPrice = v.findViewById(R.id.tv_price)
            viewHold.tvNum = v.findViewById(R.id.tv_num)
            viewHold.llDetails = v.findViewById(R.id.ll_details)
            v.tag = viewHold
        } else {
            v = convertView
            viewHold = v.tag as ViewHold
        }
        viewHold.tvPrice?.text = if (subcategory.desc!!.isEmpty()) {
            "暂无"
        } else {
            subcategory.desc
        }
        viewHold.tvName?.text = subcategory.title
        Glide.with(context)
            .load(subcategory.imgURL)
            .placeholder(R.drawable.ic_launcher_background)
            .into(viewHold.ivIcon!!)
        viewHold.tvNum?.text = "0"
        viewHold.ivJian?.setOnClickListener {
            var a = viewHold.tvNum?.text.toString().toInt()
            if (a > 0) {
                a -= 1
                viewHold.tvNum?.text = a.toString()
                subcategory.quantity = a.toString()
            }
            if (a >= 0) {
                if (subcategory.desc!!.isEmpty()) {
                    subcategory.totalPrice = 0.toString()
                } else {
                    val p = Pattern.compile("\\d+")
                    val m = p.matcher(subcategory.desc)
                    m.find()
                    subcategory.totalPrice = (m.group().toString().toInt() * a).toString()
                }
            }
        }
        viewHold.ivJia?.setOnClickListener {
            var a = viewHold.tvNum?.text.toString().toInt()
            a += 1
            viewHold.tvNum?.text = a.toString()
            subcategory.quantity = a.toString()
            if (a >= 0) {
                if (subcategory.desc.isEmpty()) {
                    subcategory.totalPrice = 0.toString()
                } else {
                    val p = Pattern.compile("\\d+")
                    val m = p.matcher(subcategory.desc)
                    m.find()
                    subcategory.totalPrice = (m.group().toString().toInt() * a).toString()
                }
                mListener?.itemClick(subcategory.totalPrice.toInt())
            }
        }
        viewHold.llDetails?.setOnClickListener {
            mLLListener?.itemClick(subcategory)
        }

        return v!!
    }

    inner class ViewHold {
        var tvNum: TextView? = null
        var tvName: TextView? = null
        var tvPrice: TextView? = null
        var ivIcon: ImageView? = null
        var ivJia: ImageView? = null
        var ivJian: ImageView? = null
        var llDetails: LinearLayout? = null
    }

    var mListener: InnerItemOnclickListener? = null

    interface InnerItemOnclickListener {
        fun itemClick(item: Int)
    }

    fun setOnInnerItemOnClickListener(listener: InnerItemOnclickListener) {
        mListener = listener
    }

    var mLLListener: LLInnerItemOnclickListener? = null

    interface LLInnerItemOnclickListener {
        fun itemClick(item: DataX)
    }

    fun setLLOnInnerItemOnClickListener(listener: LLInnerItemOnclickListener) {
        mLLListener = listener
    }

}
